package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.StuWork;
import com.ketangpai.admin.entity.dto.StuWorkDTO;
import com.ketangpai.admin.entity.param.StuWorkParam;
import com.ketangpai.admin.service.StuWorkService;
import com.ketangpai.admin.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 学生作业表(StuWork)表控制层
 *
 * @author makejava
 * @since 2023-06-14 02:10:39
 */
@RestController
@RequestMapping("stuWork")
@Api(tags = "学生作业")
public class StuWorkController {
    /**
     * 服务对象
     */
    @Resource
    private StuWorkService stuWorkService;

    /**
     * 通过主键查询单条数据
     */
    @ApiOperation("根据id查询")
    @GetMapping("/selectById/{id}")
    public JsonResult<?> selectById(@PathVariable Integer id) {
        StuWork stuWork = stuWorkService.queryById(id);
        return JsonResult.success(stuWork);
    }

    @ApiOperation("根据用户id查询")
    @GetMapping("/selectByUid/{id}")
    public JsonResult<?> selectByUid(@PathVariable Integer id) {
        List<StuWork> stuWork = stuWorkService.queryByUid(id);
        return JsonResult.success(stuWork);
    }

    @ApiOperation("根据用户、课/stuWork/update程id查询作业")
    @GetMapping("/selectByUCid/{cid}/{uid}")
    public JsonResult<?> queryByUCid(@PathVariable Integer cid, @PathVariable Integer uid) {
        List<StuWorkDTO> stuWork = stuWorkService.queryByUCid(cid, uid);
        System.out.println("stuWorks:"+stuWork);
        return JsonResult.success(stuWork);
    }

    @ApiOperation("列表查询")
    @PostMapping("/selectList")
    public JsonResult<?> selectList(@RequestBody StuWorkParam param) {
        Map<String, Object> map = stuWorkService.selectList(param);
        return JsonResult.success(map);
    }

    @ApiOperation("催交")
    @GetMapping("/expedite/{id}")
    public JsonResult<?> expedite(@PathVariable Integer id) {
        boolean result = stuWorkService.expedite(id);
        if (result) {
            return JsonResult.success("催交成功");
        }
        return JsonResult.error("催交失败");
    }

    @ApiOperation("一键催交")
    @PostMapping("/expediteBatch")
    public JsonResult<?> expediteBatch() {
        boolean result = stuWorkService.expediteBatch();
        if (result) {
            return JsonResult.success("催交成功");
        }
        return JsonResult.error("催交失败");
    }

    /**
     * 新增数据
     */
    @ApiOperation("添加")
    @PostMapping("/insert")
    public JsonResult<?> insert(StuWork stuWork) {
        boolean result = stuWorkService.insert(stuWork);
        if (result) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    public JsonResult<?> delete(@PathVariable Integer id) {
        boolean result = stuWorkService.deleteById(id);
        if (result) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public JsonResult<?> update(StuWork stuWork) {
        boolean result = stuWorkService.update(stuWork);
        if (result) {
            return JsonResult.success("编辑成功");
        }
        return JsonResult.error("编辑失败");
    }

    @ApiOperation("批量修改")
    @PostMapping("/updateBatch")
    public JsonResult<?> updateBatch(@RequestBody List<StuWork> list) {
        boolean result = stuWorkService.updateBatchById(list);
        if (result) {
            return JsonResult.success("编辑成功");
        }
        return JsonResult.error("编辑失败");
    }
}

